package xyz.itpath.gen.mapper;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.itpath.gen.autoconfigure.ProjectConfiguration;

/**
 * @author zhaom
 * @date 2021/9/22 - 21:34
 * <br/>
 */
@Configuration
public class MapperConfig {

    private static final String MYSQL = "mysql";
    private static final String ORACLE = "oracle";

    private final MySqlMateMapper mySqlMateMapper;
    private final OracleMateMapper oracleMateMapper;
    private final ProjectConfiguration projectConfiguration;

    public MapperConfig(MySqlMateMapper mySqlMateMapper,
                        OracleMateMapper oracleMateMapper,
                        ProjectConfiguration projectConfiguration) {
        this.mySqlMateMapper = mySqlMateMapper;
        this.oracleMateMapper = oracleMateMapper;
        this.projectConfiguration = projectConfiguration;
    }

    @Bean
    public GeneralMateMapper generalMateMapper() {
        String dbType = projectConfiguration.getTypeDb();
        if (MYSQL.equals(dbType)) {
            return mySqlMateMapper;
        } else if (ORACLE.equals(dbType)) {
            return oracleMateMapper;
        } else {
            throw new RuntimeException("不支持的数据库类型");
        }
    }
}
